package com.atguigu.crowd.funding.handler;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.crowd.funding.entity.ResultEntity;
import com.atguigu.crowd.funding.entity.Role;
import com.atguigu.crowd.funding.service.api.RoleService;
import com.github.pagehelper.PageInfo;

@RestController
public class RoleHandler {

	@Autowired
	private RoleService roleService; 
	
	/**
	 * 带分页查询查询
	 * 返回json
	 */
	@RequestMapping("/role/query/page")
	public ResultEntity<PageInfo<Role>> queryRoleWithPage(
			@RequestParam(name="pageNum",defaultValue="1") Integer pageNum,
			@RequestParam(name="pageSize",defaultValue="5") Integer pageSize,
			@RequestParam(name="keyWord",defaultValue="") String keyWord
			){
		PageInfo<Role> pageInfo=roleService.queryRoleBykeyWord(pageNum, pageSize, keyWord);
		
		return ResultEntity.successWithoutData(pageInfo);
	}
	
	/**
	 * 批量删除（批量删除与单选删除一起）
	 */
	@RequestMapping("/role/batch/remove")
	public ResultEntity<String> batchRemove(
			@RequestBody List<Integer> roleidList){
		try{
		roleService.batchDelete(roleidList);
		return ResultEntity.successWithoutData();
		}catch(Exception e){
		e.printStackTrace();
		return ResultEntity.failed(null, e.getMessage());
		}
	}
	/**
	 * update
	 */
	@RequestMapping("/role/update")
	public ResultEntity<String> updateRole(Role role){
		roleService.updateRole(role);
		//double k=1/0;
		return ResultEntity.successWithoutData();
	}
	/**
	 * add
	 */
	@RequestMapping("/role/add")
	public ResultEntity<String> saveRole(Role role){
		roleService.save(role);
		return ResultEntity.successWithoutData();
	}
	
}
